package owlapps2012.owlToSql.base;

import java.util.HashSet;

public class IntNode
{
	private HashSet<IntNode> childrenNodes = new HashSet<IntNode>();


	private Integer value;
	private Integer cumulatedMaxValue;
	private Integer cumulatedMinValue;
	
	public IntNode(int value) 
	{
		this.value = value;
	}

	public void accumulateValuesOfChildren()
	{
		if(value == null)
			throw new NullPointerException("No value has been set!");
		
		if(!this.childrenNodes.isEmpty())
		{
			for(IntNode childNode : this.childrenNodes)
			{
				childNode.accumulateValuesOfChildren();
				Integer localMax = this.value + childNode.getCumulatedMaxValue();
				Integer localMin = this.value + childNode.getCumulatedMinValue();
		
				if(this.cumulatedMaxValue == null)
					this.cumulatedMaxValue = localMax;
				else if(localMax > this.cumulatedMaxValue)
					this.cumulatedMaxValue = localMax;
					
				if(this.cumulatedMinValue == null)
					cumulatedMinValue = localMin;
				else if(localMin < this.cumulatedMinValue)
					this.cumulatedMinValue = localMin;
				
			}
		}
		else
		{
			this.cumulatedMaxValue = this.value;
			this.cumulatedMinValue = this.value;
		}
	}
	
//	public void accumulateValuesOfChildren()
//	{
//		if(!this.childrenNodes.isEmpty())
//		{
//			Integer localMax = null;
//			Integer localMin = null;
//			for(IntNode childNode : this.childrenNodes)
//			{
//				childNode.accumulateValuesOfChildren();
//				Integer cMax = this.value + childNode.getCumulatedMaxValue();
//				Integer cMin = this.value + childNode.getCumulatedMinValue();
//				if(localMax == null)
//					localMax = cMax;
//				else if(cMax > localMax)
//					localMax = cMax;
//				
//				if(localMin == null)
//					localMin = cMin;
//				else if(cMin < localMin)
//					localMin = cMin;
//				
//			}
//		}
//	}
	
	public Integer getValue() {
		return value;
	}

	public void setValue(Integer value) {
		this.value = value;
	}

	public Integer getCumulatedMaxValue() {
		return cumulatedMaxValue;
	}

	public void setCumulatedMaxValue(Integer cumulatedMaxValue) {
		this.cumulatedMaxValue = cumulatedMaxValue;
	}

	public Integer getCumulatedMinValue() {
		return cumulatedMinValue;
	}

	public void setCumulatedMinValue(Integer cumulatedMinValue) {
		this.cumulatedMinValue = cumulatedMinValue;
	}

	public HashSet<IntNode> getChildrenNodes() {
		return childrenNodes;
	}
	public void addChildNode(IntNode n)
	{
		this.childrenNodes.add(n);
	}
	public void addChildNode(int n)
	{
		this.childrenNodes.add(new IntNode(n));
	}
	
}
